﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Xceed.Wpf.Toolkit;

namespace WPFSceneryEditor.CustomControls
{
    /// <summary>
    /// Interaktionslogik für ValueBar.xaml
    /// </summary>
    public partial class ValueBar : UserControl
    {
        #region Private Fields

        private bool duringDrawing = false;
        private double minValue, maxValue, value;

        #endregion Private Fields

        #region Public Constructors

        public ValueBar()
        {
            InitializeComponent();

            MaxValue = 200;
            MinValue = 10;
            Value = 55;
        }

        #endregion Public Constructors

        #region Public Properties

        public double MaxValue
        {
            get { return maxValue; }
            set
            {
                maxValue = value;
                this.numericUpDown.Maximum = this.maxValue;
                UpdateGraphic(this.value);
            }
        }

        public double MinValue
        {
            get { return minValue; }
            set
            {
                minValue = value;
                this.numericUpDown.Minimum = this.minValue;
                UpdateGraphic(this.value);
            }
        }

        public double Value
        {
            get { return this.value; }
            set
            {
                this.value = Math.Max(Math.Min(value, this.maxValue), this.minValue);
                UpdateGraphic(this.value);
            }
        }

        #endregion Public Properties

        #region Private Methods

        private double GetValueFromStackPanel(MouseEventArgs e)
        {
            double gridSize = this.grid.ColumnDefinitions[0].ActualWidth;
            return e.GetPosition(stackPanel).X / gridSize * (maxValue - minValue) + minValue;
        }

        private void numericUpDown_ValueChanged_1(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            DoubleUpDown dud = (DoubleUpDown)sender;
            if (!this.duringDrawing)
                this.Value = (double)dud.Value;
        }

        private void OnMouseClick(object sender, MouseButtonEventArgs e)
        {
            this.Value = GetValueFromStackPanel(e);
        }

        private void OnMouseLeave(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                this.Value = GetValueFromStackPanel(e);

            UpdateGraphic(this.Value);
        }

        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            StackPanel stack = (StackPanel)sender;
            UpdateGraphic(GetValueFromStackPanel(e));
        }

        private void OnSizeChange(object sender, SizeChangedEventArgs e)
        {
            UpdateGraphic(value);
        }

        private void OnSliderValueChange(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Slider slider = (Slider)sender;
            Xceed.Wpf.Toolkit.MessageBox.Show(slider.Value.ToString());
        }

        private void UpdateGraphic(double value)
        {
            double gridSize = this.grid.ColumnDefinitions[0].ActualWidth;
            this.duringDrawing = true;
            this.numericUpDown.Value = value;
            value = value - this.minValue;

            double deltaMinMax = maxValue - minValue;
            //value = (1.0 / deltaMinMax) * value * this.stackPanel.Width;
            double origin;
            if (minValue < 0)
                origin = (deltaMinMax - maxValue) / deltaMinMax * gridSize;
            else
                origin = 0;

            double rectangleLength = (((value) / deltaMinMax) * gridSize);

            if (origin <= rectangleLength)
            {
                this.rectangle.Margin = new Thickness(origin, 0, 0, 0);
                this.rectangle.Width = rectangleLength - origin;
            }
            else
            {
                this.rectangle.Margin = new Thickness(rectangleLength, 0, 0, 0);
                this.rectangle.Width = origin - rectangleLength;
            }
            this.zeroLine.X1 = origin;
            this.zeroLine.X2 = origin;

            this.duringDrawing = false;
        }

        #endregion Private Methods
    }
}